package com.example.zyfypt516wh.apdater;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.activities.ViewVideoActivity;
import com.example.zyfypt516wh.bean.ResBean;
import com.example.zyfypt516wh.common.Common;
import com.squareup.picasso.Picasso;

import java.util.List;

public class VideoAdapter extends RecyclerView.Adapter{
    private List<ResBean> list;//列表数据
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态布局

    //自定义 构造方法
    public VideoAdapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }

    //自定义 ViewHolder 子类
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView tvtitle,tvauthor,tvtime;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imageView);
            tvtitle=itemView.findViewById(R.id.textView);
            tvauthor=itemView.findViewById(R.id.textView2);
            tvtime=itemView.findViewById(R.id.textView3);
        }
    }

    //自定义 设置list
    public void setList(List<ResBean> list)
    {
        this.list=list;
        notifyDataSetChanged();
    }
    @NonNull
    @Override   //重写
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item, parent, false);
        return new ViewHolder(v);
    }

    @Override   //重写    绑定控件和数据
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ResBean bean=list.get(position);
        if(bean==null)  return;
        ViewHolder viewHolder=(ViewHolder) holder;
        //资源图片
        Picasso.get()
                .load(Common.IMAGEURL+bean.getThumb())
                .placeholder(R.mipmap.ic_launcher)//图片不存在的占位图片
                .into(viewHolder.imageView);
        viewHolder.tvtitle.setText(bean.getName());//资源标题
        viewHolder.tvauthor.setText(bean.getAuthor());//资源作者
        if(bean.getUpdate_Time()==null)
            viewHolder.tvtime.setText("未知时间");
        viewHolder.tvtime.setText(bean.getUpdate_Time());//资源时间

        //item 条目点击事件
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View view) {
                //取出当前 item 的 id
                String id=bean.getId();
                Toast.makeText(context, ""+id, Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(context, ViewVideoActivity.class);
                intent.putExtra("videopath",bean.getVideopath());
                intent.putExtra("name",bean.getName());
                intent.putExtra("resid",bean.getId());
                intent.putExtra("userid",bean.getUserid()); context.startActivity(intent);
            }
        });
    }

    @Override   //重写    列表数据个数
    public int getItemCount() {
        if(list!=null)
            return list.size();
        else return 0;
    }
}
